package com.demo2do.fms.admin.dao.authority;

import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Execute;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.demo2do.fms.entity.authority.User;

/**
 * 
 * @author Ysy
 */
@Dao("hibernate")
public interface UserDao {
    
    /**
     * 
     * @param authority1
     * @param authority2
     * @return
     */
    @Query("SELECT count(*) FROM User user WHERE (user.authority LIKE :authority1 OR user.authority LIKE :authority2) AND disabled = false AND fmsPlus = true")
    public int count(@Variable("authority1") String authority1, @Variable("authority2") String authority2);
    
    /**
     * 
     * @param authority
     * @return
     */
    @Query("SELECT count(*) FROM User user WHERE user.authority LIKE :authority AND disabled = false AND fmsPlus = true")
    public int count(@Variable("authority") String authority);
    
    /**
     * @param loginName
     * @return
     */
    @Query("SELECT count(*) FROM User WHERE loginName = :loginName AND disabled = false")
    public int countByLoginName(@Variable("loginName") String loginName);
    
    /**
     * @return
     */
    @Query("FROM User user WHERE disabled = false AND fmsPlus = true")
    public List<User> list();
    
    /**
     * 
     * @param authority1
     * @param authority2
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM User user WHERE (user.authority LIKE :authority1 OR user.authority LIKE :authority2) AND disabled = false AND fmsPlus = true")
    public List<User> list(@Variable("authority1") String authority1, @Variable("authority2") String authority2, @Variable("beginIndex")int beginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * 
     * @param authority
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM User user WHERE user.authority LIKE :authority AND disabled = false AND fmsPlus = true")
    public List<User> list(@Variable("authority") String authority, @Variable("beginIndex")int beginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * @param supplierId
     * @return
     */
    @Query("FROM User WHERE supplierId = :supplierId AND disabled = false AND fmsPlus = true")
    public List<User> list(@Variable("supplierId") Long supplierId);
    
    /**
     * 
     * @param user
     */
    @Execute("DELETE FROM UserSupplierRelation s WHERE s.user = :user")
    public void deleteUserSupplierRelation(@Variable("user") User user);
    
    /**
     * 
     * @param loginName
     * @return
     */
    @Query("FROM User WHERE loginName = :loginName AND disabled = false AND fmsPlus = true")
    public User get(@Variable("loginName") String loginName);
    
    /**
     * list Ids by user
     * 
     * @param user
     * @return
     */
    @Query("SELECT s.supplierId FROM UserSupplierRelation s WHERE s.user = :user")
    public List<Long> listUserOrgnization(@Variable("user") User user);
    
}
